import java.awt.print.PrinterJob;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by IntelliJ IDEA.
 * User: xsicrz
 * Date: 5/31/11
 * Time: 10:59 AM
 * To change this template use File | Settings | File Templates.
 */
public class Model implements Serializable {

    //String=jour_mois_annee
    private Map<String, Saisie> saisiesMap = new HashMap<String, Saisie>();


    public void addSaisie(Saisie saisie) {
        String key = saisie.getDayOfMonth() +"_"+saisie.getMonth()+"_"+saisie.getYear();
        saisiesMap.put(key, saisie);
    }

     public Saisie getSaisie(int day, int month, int annee) {
        String key = day +"_"+month+"_"+annee;
         return saisiesMap.get(key);
    }

    public static void serialize(Model model) throws IOException {
        File file = new File("data.txt");
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(file));
        objectOutputStream.writeObject(model);
        objectOutputStream.flush();
        objectOutputStream.close();

    }

    public static Model deserialize() throws IOException, ClassNotFoundException {
        File file = new File("data.txt");
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(file));
        Model model = (Model)objectInputStream.readObject();
        return model;
    }

    public long getDifferenceForMonthAndYear(List<Date> daysOfMonth) {
        long totalDifferenceEnMillis = 0;
        for(Date dayOfMonth: daysOfMonth) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(dayOfMonth);
            int day = cal.get(Calendar.DAY_OF_MONTH);
            int month = cal.get(Calendar.MONTH);
            int year = cal.get(Calendar.YEAR);
            Saisie saisie = getSaisie(day, month, year);
            if(saisie != null) {
                totalDifferenceEnMillis+=saisie.getDifferenceEnMillis();
            }
        }

        return totalDifferenceEnMillis;
    }

    public long getTotalForMonthAndYear(List<Date> daysOfMonth) {
        long totalEnMillis = 0;
        for(Date dayOfMonth: daysOfMonth) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(dayOfMonth);
            int day = cal.get(Calendar.DAY_OF_MONTH);
            int month = cal.get(Calendar.MONTH);
            int year = cal.get(Calendar.YEAR);
            Saisie saisie = getSaisie(day, month, year);
            if(saisie != null) {
                totalEnMillis+=saisie.getTempsTotalEnMillis();
            }
        }

        return totalEnMillis;
    }

    public String humanFormatDifferenceForMonthAndYear(List<Date> daysOfMonth){
        long milliseconds =  getDifferenceForMonthAndYear(daysOfMonth);
        long hours = TimeUnit.MILLISECONDS.toHours(milliseconds);
        long hoursInMillis = hours * 60 * 60 * 1000;
        long remaingMinutesInMillis = milliseconds - hoursInMillis;
        long minutes = TimeUnit.MILLISECONDS.toMinutes(remaingMinutesInMillis);
        if(hours == 0) {
                return minutes+"min";
            }
            if(hours < 0) {
                if(minutes < 10 && minutes > -10) {
                    return hours+"h0"+(-minutes);
                } else {
                    return hours+"h"+(-minutes);
                }
            }
            if(minutes < 10) {
                return hours+"h0"+minutes;
            } else {
                return hours+"h"+minutes;
            }
    }

     public String humanFormatTotalForMonthAndYear(List<Date> daysOfMonth){
        long milliseconds = getTotalForMonthAndYear(daysOfMonth);
        long hours = TimeUnit.MILLISECONDS.toHours(milliseconds);
        long hoursInMillis = hours * 60 * 60 * 1000;
        long remaingMinutesInMillis = milliseconds - hoursInMillis;
        long minutes = TimeUnit.MILLISECONDS.toMinutes(remaingMinutesInMillis);
        if(hours == 0) {
                return minutes+"min";
            }
            if(hours < 0) {
                if(minutes < 10 && minutes > -10) {
                    return hours+"h0"+(-minutes);
                } else {
                    return hours+"h"+(-minutes);
                }
            }
            if(minutes < 10) {
                return hours+"h0"+minutes;
            } else {
                return hours+"h"+minutes;
            }
    }
}
